package com.liws.hellojuc.comdownlath;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CountDownLatch;

/**
 * @author ：liweisheng
 * @date ：2022/2/27 9:17
 * @description：线程同步，让一个或多个线程等待一组操作完成
 */
public class CountDownLatchTest01 {

    private static int num=10;
    public static void main(String[] args) {

        test01();
        System.out.println("--------");
       // test02();
       // System.out.println("--------");

    }

    public static void test01(){
        CountDownLatch countDownLatch = new CountDownLatch(num);
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:SS k");

        for (int i=0;i<num;i++){
            new Thread(()->{
                LocalDateTime currentTime = LocalDateTime.now();
                String landing = currentTime.format(format);
                System.out.println(Thread.currentThread().getName()+"，时间：："+landing);
                countDownLatch.countDown();
            }).start();
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void test02(){
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:SS k");

        for (int i=0;i<num;i++){
            new Thread(()->{
                LocalDateTime currentTime = LocalDateTime.now();
                String landing = currentTime.format(format);
                System.out.println(Thread.currentThread().getName()+"，时间：："+landing);
            }).start();
        }

    }
}
